package main

import "fmt"

func lengthOfLongestSubstring(s string) int {
	i, j := 0, 0
	c := make(map[byte]int)
	m := 0
	for ; i < len(s); i++ {
		if v, ok := c[byte(s[i])]; ok && v >= j {
			m = Max(m, i-j)
			j = v + 1
		}
		c[byte(s[i])] = i
	}

	return Max(m, len(s)-j)
}

func main() {
	fmt.Println(lengthOfLongestSubstring("abcabcbb"))
}

func Max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}
